/* eslint-disable */

/**
 * 数组升序排列
 */
Array.prototype.ascSort = function (key) {
  let a = this;
  let i, j, t;
  i = j = t = 0;
  for (i = 0; i < a.length; i++) {
    for (j = 0; j < a.length; j++) {
      if (key != undefined) {
        let vi = a[i][key], vj = a[j][key];

        if (typeof vi == 'string' && typeof vj == 'string') {
          if (vi.localeCompare(vj) < 0) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
          }
        } else {
          if (vi < vj) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
          }
        }
      } else {
        let vi = a[i], vj = a[j];
        if (typeof vi == 'string' && typeof vj == 'string') {
          if (vi.localeCompare(vj) < 0) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
          }
        } else {
          if (vi < vj) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
          }
        }
      }

    }
  }
  return a;
};

/**
 * 数组降序排列
 */
Array.prototype.descSort = function (key) {
  let a = this;
  let i, j, t;
  i = j = t = 0;
  for (i = 0; i < a.length; i++) {
    for (j = 0; j < a.length; j++) {
      if (key != undefined) {
        let vi = a[i][key], vj = a[j][key];

        if (typeof vi == 'string' && typeof vj == 'string') {
          if (vi.localeCompare(vj) > 0) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
          }
        } else {
          if (vi > vj) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
          }
        }
      } else {
        let vi = a[i], vj = a[j];
        if (typeof vi == 'string' && typeof vj == 'string') {
          if (vi.localeCompare(vj) > 0) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
          }
        } else {
          if (vi > vj) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
          }
        }
      }

    }
  }
  return a;
};


/* @description 数值类型处理
* @param floatvar 原始数值
* @param _len 保留小数位数
* @param valid  boolean 是否保留有效数字
* @returns {*}
*/
Math.twoBit = function (floatvar, _len, valid) {
  let f_x = parseFloat(floatvar);
  if (isNaN(f_x)) {
    return '0.00';
  }
  let len = _len || 2;  //默认保留两位
  let temp = 1;
  for (let i = 0; i < len; i++) {
    temp = temp * 10;
  }

  //保留有效数字
  if (valid === true) {
    let f_xStr = f_x + "";

    if (f_xStr.indexOf("0.00792") != -1) {
    }


    let tempArr = f_xStr.split(".");
    if (tempArr.length == 2) {
      let suff = tempArr[1];
      let validLen = 0;
      let notZ = 0;
      for (let i = 0; i < suff.length; i++) {
        if (suff[i] != "0") {
          notZ++;
          if (notZ > 1) {
            break;
          } else {
            validLen++;
          }
        } else {
          if (notZ > 0) {
            notZ++
          }
          if (notZ > 1) {
            break;
          }

          validLen++;
        }
      }

      if (validLen > _len) {
        validLen++;
        temp = 1;
        for (let i = 0; i < validLen; i++) {
          temp = temp * 10;
        }
      }
    }
  }
  f_x = Math.round(f_x * temp) / temp;

  let s_x = f_x.toString();
  let pos_decimal = s_x.indexOf('.');

  if (s_x.indexOf("Infinity") != -1) {
    return s_x;
  } else {
    if (pos_decimal < 0) {
      pos_decimal = s_x.length;
      s_x += '.';
    }
    while (s_x.length <= pos_decimal + (len || 2)) {
      s_x += '0';
    }
  }

  s_x = parseFloat(s_x);

  return s_x;
};


Date.prototype.format = function (fmt) {
  let o = {
    "M+": this.getMonth() + 1,
    "d+": this.getDate(),
    "H+": this.getHours(),
    "m+": this.getMinutes(),
    "s+": this.getSeconds(),
    "S+": this.getMilliseconds()
  };
  //因为date.getFullYear()出来的结果是number类型的,所以为了让结果变成字符串型，下面有两种方法：
  if (/(y+)/.test(fmt)) {
    //第一种：利用字符串连接符“+”给date.getFullYear()+""，加一个空字符串便可以将number类型转换成字符串。
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      //第二种：使用String()类型进行强制数据类型转换String(date.getFullYear())，这种更容易理解。
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(String(o[k]).length)));
    }
  }
  return fmt;
};

Number.prototype.format = function () {
  let temp =  (this + '').split('.');
  let foo = temp[0];
  let suffix = '';
  if(temp.length==2){
    suffix = '.' +  temp[1]
  }
  let len = foo.length;
  let bar = '';
  let flag = 0;
  for (let i = len - 1; i >= 0; i--) {
    bar += foo[i];
    flag ++;
    if (flag % 3 == 0 && i!==0) {
      bar += ',';
    }
  }

  let re = '';
  for (let i = bar.length-1; i >=0 ; i--) {
    re += bar[i];
  }
  return re   +  suffix ;
};


/**
 * rem单位转px
 * @param rem
 * @returns {*}
 */
window.rem2px = (rem) => {
  let fontSize = document.documentElement.style.fontSize;
  return Math.floor(rem * fontSize.replace('px', ''));
}

/**
 * 事件节流  多用于Resize事件
 * @param fun
 * @param delay
 * @returns {Function}
 */
window.throttle = (func, wait, options) => {
  let context, args, result;
  let timeout = null;
  let previous = 0;
  options || (options = {});
  const later = () => {
    previous = options.leading === false ? 0 : Date.now();
    timeout = null;
    result = func.apply(context, args);
    context = args = null;
  };
  return () => {
    let now = Date.now();
    if (!previous && options.leading === false) previous = now;
    let remaining = wait - (now - previous);
    context = this;
    args = arguments;
    if (remaining <= 0) {
      clearTimeout(timeout);
      timeout = null;
      previous = now;
      result = func.apply(context, args);
      context = args = null;
    } else if (!timeout && options.trailing !== false) {
      timeout = setTimeout(later, remaining);
    }
    return result;
  };
};

/**
 * 多用于输入框自动提示
 * @param func
 * @param wait
 * @param immediate
 * @returns {function(): *}
 */
window.debounce = (func, wait, immediate) => {
  let timeout, args, context, timestamp, result;

  const later = () => {
    let last = Date.now() - timestamp;
    if (last < wait) {
      timeout = setTimeout(later, wait - last);
    } else {
      timeout = null;
      if (!immediate) {
        result = func.apply(context, args);
        context = args = null;
      }
    }
  };

  return () => {
    context = this;
    args = arguments;
    timestamp = Date.now();
    let callNow = immediate && !timeout;
    if (!timeout) {
      timeout = setTimeout(later, wait);
    }
    if (callNow) {
      result = func.apply(context, args);
      context = args = null;
    }

    return result;
  };
}

/**
 * 全局工具方法
 * @type {{}}
 */
window.AppTools = {
  /**
   * 全屏
   * @param element
   * @param IsFull
   */
  fullScreen: function (element, IsFull) {
    if (IsFull) {
      if (element.requestFullscreen) {
        element.requestFullscreen();
      } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
      } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
      } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullScreen();
      }
    } else {
      if (document.exitFullscreen) {
        document.exitFullscreen();
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
      } else if (document.msExitFullscreen) {
        document.msExiFullscreen();
      } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
      }
    }

  }
}









window.css = function (el, css) {
  for (let key in css) {
    let k = key.replace(/([A-Z])/g, "-$1").toLowerCase();
    el.style[k] = css[key];
  }
}